﻿/*
 *
 * Copyright (c) 1999-2012 Vience Yin (http://www.fayea.com)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * Version 1.2.4
 *
 *
 */
 /*
	JAVASCRIPT OBJECT PROTPTYPE EXTEND 
*/
if(!Array.prototype.indexOf){
	Array.prototype.indexOf = function( v, b, s ) {
		for( var i = +b || 0, l = this.length; i < l; i++ ) {
			if( this[i]===v || s && this[i]==v ) { return i; }
		}
		return -1;
	};
}
if(!Array.prototype.unique){
	Array.prototype.unique = function( b ) {
		var a = [], i, l = this.length;
		for( i=0; i<l; i++ ) {
			if( a.indexOf( this[i], 0, b ) < 0 ) { a.push( this[i] ); }
		}
		return a;
	};
}

function processVerfiyForm(formID){
	
	var formID = (formID != undefined)?formID:"";
	
	var check = true;
	
	$(":input[verification]", "form#" + formID).each(function(){
		var rule = $(this).removeClass("typeError").attr("verification");
		var value = $(this).val();
		switch(rule) {
			case "required": if (!_isNotNull(value)) { check = false; $(this).addClass("typeError").focus(); return false;}; break;
			case "integer": if (!_isInteger(value)) { check = false; $(this).addClass("typeError").focus(); return false;}; break;
			case "double": if (!_isDouble(value)) { check = false; $(this).addClass("typeError").focus(); return false;}; break;
			case "email": if (!_isEmail(value)) { check = false; $(this).addClass("typeError").focus(); return false;}; break;
			case "date": if (!_isDateTime(value)) { check = false; $(this).addClass("typeError").focus(); return false;}; break;
			case "time": if (!_isTime(value)) { check = false; $(this).addClass("typeError").focus(); return false;}; break;
			case "idcard": if (!_isIDCard(value)) { check = false; $(this).addClass("typeError").focus(); return false;}; break;
			case "score": if (!_isScore(value)) { check = false; $(this).addClass("typeError").focus(); return false;}; break;
			
			case "password":
				var password = document.getElementById("password").value;
				var password2 = document.getElementById("password2").value;

				if ( password!=password2 ) {
					alert("两次密码不一致");
					check = false; 
					$(this).attr("password", "password").addClass("typeError").focus(); 
					return false;
				}
				break;
			
			case "group": 
			
				var checkBoxsName = $(this).attr("name");
				
				var $checkBox = $(":checkbox:checked[name='" +checkBoxsName+ "']");
				
				if (!$checkBox.length) {
					alert("请至少选择一个选项");
					check = false; $(this).attr("checked", "checked").focus(); return false;
				}
				break;
			
			case "ISBN":
				
				var isbn = value.replace(/-/g,"").toUpperCase();
				var re = /^(97(8|9))?\d{9}(\d|X)$/;
				
				if (re.test(isbn) && ( _isISBN10(isbn) || _isISBN13(isbn) )) {
					/* ISBN passed */
				}
				else {
					check = false; 
					$(this).addClass("typeError").focus(); 
					return false;
				}
				
				break;
			
		}
	});
	
	return check;
}

/*
	Customize function
*/

function _isISBN10(string) {
	if (string == undefined) return false;

	var i = 0;
	var a = 0;
	var b = "";

	for (i = 0; i < 10; i++) {
		b = string.charAt(i);
		if ( b == "X" ) { b = 10; }
		a += parseInt(b) * (10-i);
		
		//alert(a);
	}
	
	return (a % 11 == 0);
}

function _isISBN13(string) {
	if (string == undefined) return false;
	
	var check = 0;
	var i = 0;

    for (i = 0; i < 13; i+=2) check += parseInt(string.substr(i, 1));
    for (i = 1; i < 12; i+=2) check += 3 * parseInt(string.substr(i, 1));

    return check % 10 == 0;
}

function _isNotNull(string) {
	if (string == undefined) return false;
	var re = /.+/;
	return re.test(string);	
}

function _isEmail(string) {
	if (string == undefined) return false;
	var re = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
	return re.test(string);	
}

function _isInteger(string) {
	if (string == undefined) return false;
	var re = /^[-\+]?\d+$/;
	return re.test(string);	
}

function _isDouble(string) {
	if (string == undefined) return false;
	var re = /^[-\+]?\d+(\.\d+)?$/;
	return re.test(string);	
}

function _isTime(string) {
	if (string == undefined) return false;
	var re = /(?:[01][0-9]|2[0-3]):[0-5][0-9](?::[0-5][0-9](?:\.\d{3})?)?/;
	return re.test(string);	
}

function _isDateTime(string, format, reObj) {
	format = format || 'yyyy-MM-dd';
	var input = string, o = {}, d = new Date();
	var f1 = format.split(/[^a-z]+/gi), f2 = input.split(/\D+/g), f3 = format.split(/[a-z]+/gi), f4 = input.split(/\d+/g);
	var len = f1.length, len1 = f3.length;
	if(len != f2.length || len1 != f4.length) return false;
	for(var i = 0; i < len1; i ++)if(f3[i] != f4[i]) return false;
	for(var i = 0; i < len; i ++) o[f1[i]] = f2[i];
	o.yyyy = s(o.yyyy, o.yy, d.getFullYear(), 9999, 4);
	o.MM = s(o.MM, o.M, d.getMonth() + 1, 12);
	o.dd = s(o.dd, o.d, d.getDate(), 31);
	o.hh = s(o.hh, o.h, d.getHours(), 24);
	o.mm = s(o.mm, o.m, d.getMinutes());
	o.ss = s(o.ss, o.s, d.getSeconds());
	o.ms = s(o.ms, o.ms, d.getMilliseconds(), 999, 3);
	if(o.yyyy + o.MM + o.dd + o.hh + o.mm + o.ss + o.ms < 0) return false;
	if(o.yyyy < 100) o.yyyy += (o.yyyy > 30 ? 1900 : 2000);
	d = new Date(o.yyyy, o.MM - 1, o.dd, o.hh, o.mm, o.ss, o.ms);
	var reVal = d.getFullYear() == o.yyyy && d.getMonth() + 1 == o.MM && d.getDate() == o.dd && d.getHours() == o.hh && d.getMinutes() == o.mm && d.getSeconds() == o.ss && d.getMilliseconds() == o.ms;
	return reVal && reObj ? d : reVal;
	function s(s1, s2, s3, s4, s5){
		s4 = s4 || 60, s5 = s5 || 2;
		var reVal = s3;
		if(s1 != undefined && s1 != '' || !isNaN(s1)) reVal = s1 * 1;
		if(s2 != undefined && s2 != '' && !isNaN(s2)) reVal = s2 * 1;
		return (reVal == s1 && s1.length != s5 || reVal > s4) ? -10000 : reVal;
	}
};

function _isIDCard(string) {
	/* 18 位身份证验证 */
	if (string == undefined) return false;
	if(string.length != 18 & string.length != 15) return false;
	var number = string.toLowerCase();
	var d, sum = 0, v = '10x98765432', w = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2], a = '11,12,13,14,15,21,22,23,31,32,33,34,35,36,37,41,42,43,44,45,46,50,51,52,53,54,61,62,63,64,65,71,81,82,91';
	var re = number.match(/^(\d{2})\d{4}(((\d{2})(\d{2})(\d{2})(\d{3}))|((\d{4})(\d{2})(\d{2})(\d{3}[x\d])))$/);
	if(re == null || a.indexOf(re[1]) < 0) return false;
	if(re[2].length == 9){
		number = number.substr(0, 6) + '19' + number.substr(6);
		d = ['19' + re[4], re[5], re[6]].join('-');				
	} else d = [re[9], re[10], re[11]].join('-');
	if(!_isDateTime(d, 'yyyy-MM-dd')) return false;
	for(var i = 0;i < 17; i++) sum += number.charAt(i) * w[i];
	return (re[2].length == 9 || number.charAt(17) == v.charAt(sum % 11));
}

function _isScore(string) {
	if (string == undefined) return false;
	var re = /^[-\+]?\d+(\.\d+)?$/;
	if (re.test(string)) {
		return (Number(string) >= 0 & Number(string) <= 100);
	}
	return false;
}
